package com.haitaoit.chengquan.module.center.activity.xuqiu;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.adapter.xuqiu.MyXuqiuDetailAdapter;
import com.haitaoit.chengquan.module.center.model.MyXuqiuDetailModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DialogUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyXuqiuDetailActivity extends BaseActivity /*implements PullToRefreshBase.OnRefreshListener<ScrollView>*/ {
    //    @ViewInject(R.id.pullrefreshscrollview)
    //    PullToRefreshScrollView pullrefreshscrollview;
    //    private ScrollView mScrollView;
    //    private LinearLayout null_information_layout;
    //    private MyListView listView;
    //    private TextView xuqiu_state;
    //    private boolean isFrist = true;
    //    private int page = 1;
    //    private View view;
    @ViewInject(R.id.scrollview_listview2)
    MyListView listView;
    @ViewInject(R.id.null_information_layout)
    LinearLayout null_information_layout;
    @ViewInject(R.id.xuqiu_state)
    TextView xuqiu_state;
    @ViewInject(R.id.xuqiu_map)
    TextView xuqiu_map;
    @ViewInject(R.id.demand_number)
    TextView demand_number;
    @ViewInject(R.id.title_img)
    ImageView title_img;
    @ViewInject(R.id.detail_title)
    TextView detail_title;
    @ViewInject(R.id.detail_type)
    TextView detail_type;
    @ViewInject(R.id.xuqiu_detail_money)
    TextView xuqiu_detail_money;


    private String id;
    private MyXuqiuDetailAdapter adapter;
    private MyXuqiuDetailModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView_V(R.layout.pullrefreshscollview);
        setContentView_V(R.layout.activity_my_xuqiu_detail);
        setTitle_V("订单详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


//        view = LayoutInflater.from(this).inflate(R.layout.activity_my_xuqiu_detail, null);
//        null_information_layout = (LinearLayout) view.findViewById(R.id.null_information_layout);
//        listView = (MyListView) view.findViewById(R.id.scrollview_listview2);
//        xuqiu_state = (TextView) view.findViewById(R.id.xuqiu_state);
//        // 地图
//        view.findViewById(R.id.xuqiu_map).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(MyXuqiuDetailActivity.this, MyXuqiuMapActivity.class));
//            }
//        });
//        TextView xuqiu_detail_money = (TextView) view.findViewById(R.id.xuqiu_detail_money);
//        pullrefreshscrollview.setPullLoadEnabled(true);
//        pullrefreshscrollview.setPullRefreshEnabled(false);
//        pullrefreshscrollview.setOnRefreshListener(this);
//        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
//                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
//        mScrollView = pullrefreshscrollview.getRefreshableView();
//        mScrollView.setFillViewport(true);
//        mScrollView.setVerticalScrollBarEnabled(false);
//        mScrollView.addView(view);

        adapter = new MyXuqiuDetailAdapter(this);
        adapter.setBackCall(backCall);

        id = getIntent().getStringExtra("order_id");
        httpGetuser_demand_model();
    }

    @OnClick({R.id.xuqiu_map,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.xuqiu_map:
                startActivity(new Intent(MyXuqiuDetailActivity.this, MyXuqiuMapActivity.class));
                break;
        }
        super.onClick(v);
    }

    BackCall backCall = new BackCall() {
        @Override
        public void deal(int which, Object... obj) {
            final int position = (Integer) obj[0];
            Intent intent = new Intent();
            switch (which) {
                case R.id.xuqiu_detail_lookinfo:// 查看详情
                    intent.setClass(MyXuqiuDetailActivity.this, MyXuqiuBaojiaActivity.class);
                    intent.putExtra("id", model.getDemand_offer_list().get(position).getId());
                    startActivity(intent);
                    break;
                case R.id.xuqiu_detail_choose:// 选他服务
                    // order_type 未报价 已报价
                    if (model.getOrder_status().equals("1")) {
                        Dialog dlg = DialogUtil.showInfoDialog2(MyXuqiuDetailActivity.this,
                                "当前报价还未结束，报价时间内你还不能选择服务商服务，请耐心等待报价结束再选择服务商服务", "", "知道了", new BackCall() {
                                    public void deal(int which, Object... obj) {
                                        switch (which) {
                                        }
                                        super.deal(which);
                                    }
                                });
                        dlg.setCanceledOnTouchOutside(true);
                        dlg.show();
                    } else if (model.getOrder_status().equals("2")) {
                        Dialog dlg = DialogUtil.showInfoDialog2(MyXuqiuDetailActivity.this,
                                "选定该服务商则需要支付¥" + model.getDemand_offer_list().get(position).getOffer_money()
                                        + "到他的账号完成服务商选择", "再想想", "去支付", new BackCall() {
                                    public void deal(int which, Object... obj) {
                                        switch (which) {
                                            case R.id.cancel:// 确定
                                                Intent intent = new Intent(MyXuqiuDetailActivity.this, PaymentActivity.class);
                                                intent.putExtra("type", "1");
                                                intent.putExtra("chioceok", "chioceok");
                                                intent.putExtra("order_no", model.getDemand_offer_list().get(position).getOrder_no());
                                                intent.putExtra("order_amount", model.getDemand_offer_list().get(position).getOffer_money());
                                                startActivity(intent);
                                                break;
                                        }
                                        super.deal(which);
                                    }
                                });
                        dlg.setCanceledOnTouchOutside(true);
                        dlg.show();
                    }
                    break;
            }
            super.deal(which, obj);
        }
    };

    // 列表
    private void httpGetuser_demand_model() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getuser_demand_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
//                List<MyXuqiuModel> new_list = new ArrayList<MyXuqiuModel>();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
//                            pullrefreshscrollview.onPullDownRefreshComplete();
//                            pullrefreshscrollview.onPullUpRefreshComplete();
//                            new_list = new ArrayList<MyXuqiuModel>();
                            break;
                        case 0:// 成功
                            model = gson.fromJson(Response, MyXuqiuDetailModel.class);
                            if (model.getOrder_status().equals("1")) {
                                xuqiu_map.setVisibility(View.GONE);
                                xuqiu_state.setText("未报价");
                            } else if (model.getOrder_status().equals("2")) {
//                                view.findViewById(R.id.xuqiu_map).setVisibility(View.VISIBLE);
                                xuqiu_map.setVisibility(View.VISIBLE);
                                xuqiu_state.setText("已报价");
                            }

                            demand_number.setText("需求编号：" + model.getDemand_number());
                            ImageLoader.getInstance().displayImage(model.getTitle_img(), title_img);
                            detail_title.setText(model.getTitle());
                            detail_type.setText("分类：" + model.getType());

                            SpannableString msp = new SpannableString("参考价格：¥ " + model.getReference_price());
                            msp.setSpan(new ForegroundColorSpan(Color.RED), 5, ("参考价格：¥ " + model.getReference_price()).length(),
                                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            msp.setSpan(new RelativeSizeSpan(1.5f), 6, ("参考价格：¥ " + model.getReference_price()).length(),
                                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            xuqiu_detail_money.setText(msp);

                            // 1未选择服务商 2已支付或已完成未评价 3已完成并且评价
                            if (model.getOrder_type().equals("1")) {
                                if (model.getDemand_offer_list() == null || model.getDemand_offer_list().size() == 0) {
                                    null_information_layout.setVisibility(View.VISIBLE);
                                    listView.setVisibility(View.GONE);
                                } else {
                                    adapter.setList(model.getDemand_offer_list());
                                    adapter.setType(model.getOrder_status());
                                    listView.setAdapter(adapter);
                                }
                            }
                            break;
//                        case 2:// 暂无内容
//                            pullrefreshscrollview.onPullDownRefreshComplete();
//                            pullrefreshscrollview.onPullUpRefreshComplete();
//                            new_list = new ArrayList<MyXuqiuModel>();
//                            break;
                    }
//                    if (isFrist) {
//                        if (isNullDate(new_list)) {
//                            adapter.setList(new_list);
//                            adapter.notifyDataSetChanged();
//                        }
//                        pullrefreshscrollview.onPullDownRefreshComplete();
//                    } else {
//                        List<MyXuqiuModel> allList = adapter.getList();
//                        allList.addAll(new_list);
//                        adapter.setList(allList);
//                        adapter.notifyDataSetChanged();
//                        pullrefreshscrollview.onPullUpRefreshComplete();
//                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

//    /**
//     * @tab 判断listview是否有值
//     */
//    private boolean isNullDate(List rewordList) {
//        if (rewordList == null || rewordList.size() == 0) {
//            null_information_layout.setVisibility(View.VISIBLE);
//            listView.setVisibility(View.GONE);
//            return false;
//        } else {
//            null_information_layout.setVisibility(View.GONE);
//            listView.setVisibility(View.VISIBLE);
//            return true;
//        }
//    }
//    @Override
//    public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
//                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
//        isFrist = true;
//        page = 1;
//    }
//    @Override
//    public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
//                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
//        isFrist = false;
//        page++;
//    }
}
